package cn.kongwg.servlet;

import cn.kongwg.bean.User;
import cn.kongwg.service.UserService;
import cn.kongwg.service.impl.UserServiceImpl;
import cn.kongwg.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author kongwg
 * @create 2021-01-25 15:26
 */
@WebServlet("/userServlet")
public class UserServlet extends BaseServlet {
    UserService userService = new UserServiceImpl();

    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if (userService.login(new User(null, username, password, null)) == null) {
            System.out.println("登录失败");
            request.setAttribute("msg", "账号或者密码错误");
            request.setAttribute("username", username);
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        } else {
            System.out.println("denglu chenggong ");
            request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request, response);
        }
    }

    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String code = req.getParameter("code");
/*
        System.out.println(username);
        System.out.println(password);
        System.out.println(email);
        System.out.println(code);*/
        Map<String, String[]> parameterMap = req.getParameterMap();

        User user = WebUtils.copyParamToBean(parameterMap, new User());
        if ("abcd".equalsIgnoreCase(code)) {
            //成功
            if (userService.existUsername(username)) {
                System.out.println("用户名[" + username + "]已存在!");
//        跳回注册页面
                req.setAttribute("msg", "用户名已存在");
                req.setAttribute("username", username);
                req.setAttribute("email", email);
                req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
            } else {
//                   可用
//                调用Sservice保存到数据库
                userService.register(user);
//
//                 跳到注册成功页面 regist_success.jsp
                req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req, resp);
            }
        } else {
            System.out.println("验证码[" + code + "]错误");
            req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
            req.setAttribute("msg", "验证码错误");
            req.setAttribute("username", username);
            req.setAttribute("email", email);
        }

    }


}
